package com.aischool.webapp.teacher;

import com.aischool.common.model.AiLifePieces;
import com.aischool.params.AiLifePiceseParams;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiLifePiceseService;
import com.aischool.utils.AppUtil;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;

@RouteMapping(url = "/front/teacher/lifepieces")
public class AiLifePiecesController extends BaseTeacherController {
	

	@Inject
	AiLifePiceseService aiLifePiceseService;

	public void getPage() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 参数类
		AiLifePiceseParams params = new AiLifePiceseParams();
		
		// 获取参数
		int login_user_id = this.getAttrForInt("login_user_id");
		int login_org_id = this.getAttrForInt("login_org_id");	
		int login_class_id = this.getAttrForInt("login_class_id");	
		
		int onlyme = getInt("onlyme", 0);

		
		params.setPageNo(getParaToInt("pageNo", 1));
		params.setPageSize(getParaToInt("pageSize", AppUtil.Default_PageSize));
		
		//params.setTypeId(type_id); 
		params.setUserId(login_org_id);
		params.setClassId(login_class_id);
		params.setState(1);
		
		if (onlyme == 1) {
			params.setUserId(login_user_id);
			params.setReadPower(0); // 0私有
		}else {
			params.setReadPower(1); // 1 公开
		}

		if (params.getPageNo() == 1) {
			// 获取教师信息
			jr.setDicKV("login_teacher_rd", this.getAttr("login_teacher_rd"));
		}
		
		// 获取分页
		Object pageData = aiLifePiceseService.getPage(params);
		jr.setDicKV("pageData", pageData);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	

	public void save() {
		JsonResult jr =new JsonResult();


		int login_user_id = this.getAttrForInt("login_user_id");
		int login_org_id = this.getAttrForInt("login_org_id");
		int login_class_id = this.getAttrForInt("login_class_id");

		
		int picese_type_id = getInt("picese_type_id");
		int student_id = getInt("student_id", 0);
		int is_baby = getInt("is_baby", 0);
		
		AiLifePieces info = getModel(AiLifePieces.class, "", true);
		
		if (picese_type_id == 2 && StrKit.isBlank(info.getPiceseInfo()) && StrKit.isBlank(info.getPiceseImgs())) {
			jr.appendMsg("请输入要发布的信息或者上传图片！");
		}else if (picese_type_id == 1 && StrKit.isBlank(info.getPiceseImgs())) {
			jr.appendMsg("请上传图片！");
		}else if (is_baby == 1 && student_id<=0) {
			jr.appendMsg("请选择学生！");
		}else {
			boolean bf = false;
			
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			
			if (info.getPiceseId() != null && info.getPiceseId()>0) {
				bf = info.update();
			}else {
				info.setAddUserid(login_user_id);
				info.setAddTime(DateUtil.getTimestamp());
				info.setOrgId(login_org_id);
				info.setClassId(login_class_id);
				info.setStudentId(student_id);
				
				info.setIsAlbum(false);
				info.setIsLife(false);
				
				if (StrKit.notBlank(info.getPiceseImgs())) {
					info.setIsAlbum(true);
				}
				if (picese_type_id == 2) {
					info.setIsLife(true);
				}
				
				info.setPiceseState(1);
				bf = info.save();
			}
			
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				String msg = jr.getMsg();
				jr.setEnum(JsonCodeEnum.Faild);
				jr.appendMsg(msg);
			}
		}
		
		renderJson(jr.Dic);
	}
	

	public void del() {
		JsonResult jr =new JsonResult();
		int login_user_id = this.getAttrForInt("login_user_id");
		boolean bf = false;
		int id = getInt("id",0);
		if (id > 0) {
			bf = aiLifePiceseService.del(id, login_user_id);
		}
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		renderJson(jr.Dic);
	}
}
